#########################################
# Replication Script: Ballots & Burials #
#########################################

# Preliminaries ----

# If you are using RStudio, you can run this to properly set the working directory
# (to the location of this script):

setwd(dirname(rstudioapi::getActiveDocumentContext()$path))

# Otherwise, manually set it to the top-level folder.

source("renv/activate.R") # make sure we are inside the right env

# To automatically install all dependencies, run this command:
renv::restore()

# The scripts create a lot of temporary objects we do not want to keep
# around (our global env would get too large):

source_temp <- function(file) {
  # source script & throw everything 
  # into local env which dies after
  # this function terminates:
  local_env <- new.env()
  source(file, local = local_env)
  invisible(NULL)
}

# Replication ----

# Generate excess mortality data (this one _has_ to be run & may take a while):

source_temp("Code/generate_excess_mortality_data.R")#
# ^ This one calls the preprocessing, estimation & postprocessing scripts
# for the variational inference routine.
gc() # clean up

# Figures

# Figure 4

source_temp("Code/fig4.R")

# Figure 6A

source_temp("Code/fig6a.R")

# Generate figures A9 & A10:

source_temp("Code/fig_A9_A10.R")
gc()

# Figure A3
source_temp("Code/figA3.R")
gc()

# Figure A8
source_temp("Code/figA8.R")
gc()

# Supplementary

# Direct Regression (writes the results to Results/mean_relative_mortality_increase.csv)

source_temp("Code/suppMatC_1_5_Direct_Regression.R")
gc()